package com.gugu.procedure.controller;


import cn.hutool.core.bean.BeanUtil;
import com.gugu.procedure.bean.InsideOrder;
import com.gugu.procedure.bean.OrderItem;
import com.gugu.procedure.bean.Parameter;
import com.gugu.procedure.common.MessageResult;
import com.gugu.procedure.service.InsideOrderService;
import com.gugu.procedure.service.ProductionRecordService;
import com.gugu.procedure.service.ReplenishmentService;
import com.gugu.procedure.util.ExcelUtil;
import com.gugu.procedure.util.OrderUtil;
import com.gugu.procedure.vo.ReplenishmentExcelVo;
import com.gugu.procedure.vo.ReplenishmentVo;
import com.gugu.procedure.vo.StaffProductionRecordExport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = {"excel导出"})
@RequestMapping("excel")
public class ExcelController {

    @Autowired
    private ProductionRecordService productionRecordService;
    @Autowired
    private InsideOrderService insideOrderService;
    @Autowired
    private ReplenishmentService replenishmentService;

    @GetMapping("open/staffProductionRecordExport")
    @ApiOperation(value = "用工列表导出")
    public void staffProductionRecordExport(HttpServletResponse response, HttpServletRequest request, String orderNo, String staffNumber, String startTime, String endTime, String production, String status) {
        MessageResult mr = productionRecordService.
                listStaffProductionRecord(orderNo, staffNumber, startTime, endTime, production, status, 1, Integer.MAX_VALUE);
        List <Map <String, Object>> mapList = mr.getTotalElement();
        List <StaffProductionRecordExport> list = new ArrayList();
        for (Map <String, Object> map : mapList) {
            StaffProductionRecordExport bean = BeanUtil.mapToBeanIgnoreCase(map, StaffProductionRecordExport.class, true);
            if ("1".equals(bean.getStatus())) {
                bean.setStatus("已完成");
            } else {
                bean.setStatus("加工中");
            }
            list.add(bean);
        }
        ExcelUtil.download(response, request, list, StaffProductionRecordExport.class, "用工信息表");
    }


    @GetMapping("open/orderExport")
    @ApiOperation(value = "订单导出")
    public void orderExport(HttpServletResponse response, String orderNo) {
        MessageResult mr = insideOrderService.insideOrderDetail(orderNo, null);
        Map <String, Object> map = (Map) mr.getData();
        InsideOrder insideOrder = (InsideOrder) map.get("insideOrder");
        List <OrderItem> orderItems = (List) map.get("orderItem");
        List <Parameter> parameters = (List) map.get("parameters");
        OrderUtil.exportOrder(response, insideOrder, orderItems, parameters);
    }


    @GetMapping("open/replenishmentExport")
    @ApiOperation(value = "导出")
    public void export(HttpServletResponse response, HttpServletRequest request, String status, String orderNo, String month, String staffName) {
//        String role = getRole();
//        if (role.contains(Constant.MANAGER_ROLE)) {
//            role = null;
//        }
        String role = null;
        MessageResult mr = replenishmentService.page(role, status, orderNo, month, staffName, 1, Integer.MAX_VALUE);
        List <ReplenishmentVo> voList = mr.getTotalElement();
        List <ReplenishmentExcelVo> voList1 = com.gugu.procedure.util.BeanUtil.copyPropsForList(voList, ReplenishmentExcelVo.class);
        ExcelUtil.download(response, request, voList1, ReplenishmentExcelVo.class, "补料申请");
    }
}
